package com.lanou.tipsy.tools.volley;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.lanou.tipsy.base.BaseApplication;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.db.DaoMaster;
import com.lanou.tipsy.db.DaoSession;
import com.lanou.tipsy.db.TipsyEntityDao;


/**
 * Created by wangshuai on 15/11/7.
 * 数据库的单例封装类
 */
public class DaoSingleton {
    private volatile static DaoSingleton instance;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private TipsyEntityDao tipsyEntityDao;

    /**
     * 构造方法私有,防止其他类中通过new创建对象
     */
    private DaoSingleton() {
        context = BaseApplication.getContext();

    }
    public static DaoSingleton getInstance() {
        if (instance == null) {
            synchronized (DaoSingleton.class) {

                if (instance == null) {
                    instance = new DaoSingleton();
                }
            }
        }
        return instance;
    }
    public DaoMaster.DevOpenHelper getHelper() {

        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, Constants.DATABASE_NAME, null);

        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());

        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public TipsyEntityDao getTipsyEntityDao() {
        if (tipsyEntityDao == null) {
            tipsyEntityDao = getDaoSession().getTipsyEntityDao();
        }
        return tipsyEntityDao;
    }

}
